package cn.edu.swust.adressbook.activity;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.edu.swust.adressbook.R;
import cn.edu.swust.adressbook.adapter.GroupItemAdapter;
import cn.edu.swust.adressbook.bean.Group;
import cn.edu.swust.adressbook.service.IBaseService;
import cn.edu.swust.adressbook.service.impl.GroupServiceImpl;

/**
 *
 */

public class GroupActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String GROUP_ITEM_CHECKED = "groupItemChecked";
    private IBaseService mBaseService;
    private List<Group> mGroups;
    private ListView mListView;
    private GroupItemAdapter mAdapter;
    private boolean mIsEditAll = false;
    private TextView mEditAllButton;
    private TextView mAddButton;
    private PopupWindow mPopWindow; ///< 新建群组的浮框
    private EditText mGroupName;  ///< 新建群组的组名输入框
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_group_list);
        mBaseService = new GroupServiceImpl(GroupActivity.this);
        getView();
        initData();
        setListener();
        setListItemOnclickListener();

    }

    private void setListItemOnclickListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!mIsEditAll) {
                    Intent intent = new Intent(GroupActivity.this, GroupDetailsActivity.class);
                    intent.putExtra(GROUP_ITEM_CHECKED, mGroups.get(i));
                    startActivity(intent);
                }
            }
        });
    }


    private void setListener() {
        findViewById(R.id.edit_button).setOnClickListener(this);
        mEditAllButton.setOnClickListener(this);
        findViewById(R.id.all_button).setOnClickListener(this);
        mAddButton.setOnClickListener(this);
    }

    private void initData() {

        mAdapter = new GroupItemAdapter(GroupActivity.this);
        mGroups = mBaseService.getAll();
        updateList();
//        mListView
    }

    private void updateList() {

        mAdapter.setGroups(mGroups);
        mListView.setAdapter(mAdapter);
    }

    private void getView() {
        mListView = (ListView) findViewById(R.id.group_list);
        mEditAllButton = (TextView) findViewById(R.id.edit_all_button);
        mAddButton = (TextView) findViewById(R.id.add_btn);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.edit_button:
            case R.id.edit_all_button:
                //TODO: to edit many list item
                editAllOrCancel();
                break;

            case R.id.add_btn:
                //TODO: intent add activity or delete
                addOrDelete();
                break;
            case R.id.all_button:
                finish();
                break;
            case R.id.input_add:
                addGroup();

                break;
            case R.id.input_cancel:
                mPopWindow.dismiss();
                break;
            default:
                break;

        }
    }

    private void addGroup() {
        String groupName = mGroupName.getText().toString();
        if ("".equals(groupName)) {
            Toast.makeText(this,"分组名不能为空",Toast.LENGTH_SHORT).show();
        } else {
            boolean isExist = false;
            for (int i = 0; i < mGroups.size(); i++) {
                if (mGroups.get(i).getName().equals(groupName)) {
                    Toast.makeText(this,"该分组名已经存在",Toast.LENGTH_SHORT).show();
                    isExist = true;
                    break;
                }
            }
            if (!isExist) {
                Group group = new Group();
                group.setName(groupName);
                mBaseService.add(group);
                mPopWindow.dismiss();
                mBaseService = new GroupServiceImpl(GroupActivity.this);
                initData();
            }

        }
    }

    private void showPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(GroupActivity.this).inflate(R.layout.activity_add_group, null);
        mPopWindow = new PopupWindow(contentView,
                ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        //设置各个控件的点击响应
        mGroupName = (EditText) contentView.findViewById(R.id.input_group_name);

        contentView.findViewById(R.id.input_add).setOnClickListener(this);
        contentView.findViewById(R.id.input_cancel).setOnClickListener(this);

        //显示PopupWindow
        View rootView = LayoutInflater.from(GroupActivity.this).inflate(R.layout.activity_group_list, null);
        mPopWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0);

    }

    private void addOrDelete() {
        if (mIsEditAll){
            //TODO: delete all of checked
            deleteChecked();
        } else {
            //TODO: add group
            showPopupWindow();
        }
    }

    private void deleteChecked() {

        long[] groupItemIds = mListView.getCheckedItemIds();
        for (int i = 0; i < groupItemIds.length; i++) {
            mBaseService.delete(mGroups.get((int)groupItemIds[i]));
        }
        initData();
        editAllOrCancel();
    }

    private void editAllOrCancel() {
        mIsEditAll = !mIsEditAll;
        if (mIsEditAll){
            mEditAllButton.setText(MainActivity.CANCEL);
            mAddButton.setText(MainActivity.DELETE);
        } else {
            mEditAllButton.setText(MainActivity.EDIT);
            mAddButton.setText(MainActivity.ADD);
        }
        mAdapter.setNeedCheck(mIsEditAll);
        updateList();
    }
}
